package com.sto.stoapp.db.manager;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import com.sto.stoapp.dataprocess.parcelable.GetTableParcel;
import com.sto.stoapp.dataprocess.parcelable.SelectTableParcel;
import com.sto.stoapp.db.StoDataBase;
import com.sto.stoapp.db.constant.StoOrderReasonConstant;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.UnifiedStaticType;

/**
 * 获取列表的数据
 * 
 * @author Administrator
 * 
 */
public class SelectTableManager extends StoDataBase {

	// private Context context;

	public SelectTableManager(Context context) {
		super(context);
		// this.context = context;
	}

	/**
	 * 搜索全部数据
	 * 
	 * @return List<OnBusinessEntity>
	 */
	public List<GetTableParcel> queryAll(SelectTableParcel Entity) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			List<GetTableParcel> data = null;
			try {
				data = getDataFromCursor(
						getCursor(UnifiedStaticType.READ_DATABASE,
								Entity.getSelectSql(), Entity.getSelectField()),
						Entity);

			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("queryAll", e.getMessage());
			}
			return data;
		}
	}
	
	/**
	 * 搜索单条数据
	 * 
	 * @return List<OnBusinessEntity>
	 */
	public GetTableParcel queryOnly(SelectTableParcel Entity) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			GetTableParcel data = null;
			try {
				 Cursor c =getCursor(UnifiedStaticType.READ_DATABASE, 
						Entity.getSelectSql(), 
						Entity.getSelectField());
				if(c != null){
					if (c.moveToFirst()) {
						data = getSingleData(c, Entity);
					}
					closeCursor(c);
				}

			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("queryOnly", e.getMessage());
			}finally {
				closeDb(UnifiedStaticType.READ_DATABASE);
			}
			return data;
		}
	}
	

	/**
	 * 通过游标获取表的数据
	 * 
	 * @param c
	 * @param Entity
	 * @return
	 */
	private List<GetTableParcel> getDataFromCursor(final Cursor c,
			SelectTableParcel Entity) {
		List<GetTableParcel> listData = new ArrayList<GetTableParcel>();
		try {
			if (c != null) {
				if (c.moveToFirst()) {
					do {
						listData.add(getSingleData(c, Entity));
					} while (c.moveToNext());
				}
				closeCursor(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ExceptionLog.writeLog("getDataFromCursor", e.getMessage());
		} finally {
			closeCursor(c);
			closeDb(UnifiedStaticType.READ_DATABASE);
		}

		return listData;
	}

	@SuppressLint("NewApi")
	private GetTableParcel getSingleData(Cursor c, SelectTableParcel Entity) {
		GetTableParcel data = new GetTableParcel();

		// 编号
		if (Entity.getSelectCode() != null) {
			data.setSelectCode(c.getString(c.getColumnIndex(Entity
					.getSelectCode())));
		} else {
			data.setSelectCode("");
		}

		// 名称
		if (Entity.getSelectName() != null) {
			if(Entity.getSelectName().equals(StoOrderReasonConstant.RECORD_CONTENT)){
				String name = c.getString(c.getColumnIndex(Entity
						.getSelectName()));
				data.setSelectName(name.substring((name.indexOf(".") + 1)));
			}else{
				data.setSelectName(c.getString(c.getColumnIndex(Entity
						.getSelectName())));
			}
		} else {
			data.setSelectName("");
		}

		// 属性
		if (Entity.getProperty() != null) {
			data.setProperty(c.getString(c.getColumnIndex(Entity.getProperty())));
		} else {
			data.setProperty("");
		}

		return data;
	}
	
}
